import datetime
import json
import threading
import time

import pymongo
import redis
import requests
import execjs
import hashlib

from concurrent.futures import ThreadPoolExecutor


class BitCoin:
    def __init__(self):
        self.redis_conn = redis.Redis(host='127.0.0.1', port=6379, db=1)
        self.mongo_client = pymongo.MongoClient(host='127.0.0.1', port=27017)
        self.collection = self.mongo_client['py_spider']['bitcoin']
        self.redis_set_key = 'bitcoin_unique_set'
        self.headers = {
            "authority": "www.oklink.com",
            "accept": "application/json",
            "accept-language": "zh-CN,zh;q=0.9",
            "devid": "de5b4b20-a6de-4eb3-81e4-ae1d5d7fc872",
            "referer": "https://www.oklink.com/cn/btc/tx-list",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
            "x-cdn": "https://static.oklink.com",
            "x-locale": "zh_CN",
        }
        with open("比特币.js", encoding="utf-8") as f:
            js_code = f.read()

        apikey = execjs.compile(js_code).call("getApiKey")
        self.headers['x-apikey'] = apikey

        self.url = "https://www.oklink.com/api/explorer/v1/btc/transactionsNoRestrict"

    def get_data(self, page):
        params = {
            "t": str(int(time.time() * 1000)),
            "offset": page,
            "limit": "20"
        }
        response = requests.get(self.url, headers=self.headers, params=params)
        result = response.json()

        for data in result['data']['hits']:
            item = dict()
            item['hash'] = data['hash']
            item['block'] = data['blockHeight']
            item['inputsCount'] = data['inputsCount']
            item['outputsCount'] = data['outputsCount']
            item['inputsValue'] = data['inputsValue']
            item['outputsValue'] = data['outputsValue']
            item['realTransferValue'] = data['realTransferValue']
            item['time'] = datetime.datetime.fromtimestamp(data['blocktime']).strftime("%Y-%m-%d %H:%M:%S")

            md5 = hashlib.md5(json.dumps(item, ensure_ascii=False).encode()).hexdigest()
            # redis去重，不存在才入库
            if self.redis_conn.sadd(self.redis_set_key, md5) != 0:
                self.collection.insert_one(item)
                print("入库成功：", item)


if __name__ == '__main__':
    b = BitCoin()
    # thread_obj_list = [threading.Thread(target=b.get_data, args=(page * 20,)) for page in range(500)]
    # for thread_obj in thread_obj_list:
    #     thread_obj.start()
    with ThreadPoolExecutor(max_workers=5) as pool:
        for page in range(500):
            pool.submit(b.get_data, page * 20)
